package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.enums.CancelOrderTypeEnum;
import com.shop2cn.iapi.sqoptrade.parameter.req.PlaceOrderReq;
import com.shop2cn.iapi.sqoptrade.parameter.req.SqStockChangeNotifyReq;
import com.shop2cn.iapi.sqoptrade.parameter.vo.OrderItemVo;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.SqStockChangeNotifyCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.BargainGroupService;
import com.ymttest.business.service.shenqi.DGSQBaseService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.common.order.userquery.util.Utils;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 批量接单 FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
// 核销接口没有权限执行
public class Ts_SqStockChangeNotify {

	private static SqStockChangeNotifyReq sqStockChangeNotifyReq;
	private static SqStockChangeNotifyCall sqStockChangeNotifyCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("库存核销");
	}

	@Before
	public void caseUp() {

		sqStockChangeNotifyReq = new SqStockChangeNotifyReq();
		sqStockChangeNotifyCall = new SqStockChangeNotifyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sqStockChangeNotifyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public Boolean checkOrderWaitInStock(long orderId) {
		
		List<OrderDetailPo> orderDetailPoList = orderShardingWapper.selectOrderDetailbyOrderId(orderId);
		
		OrderDetailPo orderDetailPo = orderDetailPoList
				.stream()
				.filter(po -> Utils.falseIfNull(po
						.getWaitInStock()) == true).findAny().orElse(null);
		
		return null == orderDetailPo ? false : true;
	}
	
	public void verify(List<Long> orderIdList, int num) {
		
		String catalogId = sqStockChangeNotifyReq.getCatalogId();
		
		for (int i = 0; i < orderIdList.size(); i++) {
			
			long orderId = orderIdList.get(i);
			Logger.comment("验证orderId：" + orderId);
			
			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);
			OrderDetailPo orderDetailPo = orderShardingWapper
					.selectOrderDetailbyOrderIdAndGoodsOwnerCatalogId(orderId,
							catalogId);
			
			int vStockCount = orderDetailPo.getvStockCount();
			
			// 如果是取消或者待支付的订单，不能核销
			if (SqTradingCallService.isCanceledOrder(orderPo) || orderPo.getOrderStatus() == 1) {
				
				Logger.verifyEquals(true, orderPo.getWaitInStock(), "验证orderPo WaitInStock");
				Logger.verifyEquals(null, orderPo.getInStockTime(), "验证orderPo InStockTime");
				Logger.verifyEquals(true, orderDetailPo.getWaitInStock(), "验证订单详情上orderDetailPo WaitInStock");
				Logger.verifyEquals(null, orderDetailPo.getInStockTime(), "验证订单详情上orderDetailPo InStockTime");
				
			} else if (num >= vStockCount){
				
				Boolean checkOrderWatitInStock = this.checkOrderWaitInStock(orderId);
				
				Logger.verifyEquals(checkOrderWatitInStock, orderPo.getWaitInStock(), "验证orderPo WaitInStock");
				
				if (checkOrderWatitInStock)
					Logger.verifyEquals(null, orderPo.getInStockTime(),
							"验证orderPo InStockTime");
				else
					Logger.verifyEquals(true,
							com.shop2cn.iapi.sqoptrade.parameter.utill.Utils.calcTimeSpanMin(orderPo
									.getInStockTime()) < 2, "验证数据库InStockTime跟系统当前时间差为2分钟内");
				
				Logger.verifyEquals(false, orderDetailPo.getWaitInStock(), "验证订单详情上orderDetailPo WaitInStock");
				Logger.verifyEquals(true,
						com.shop2cn.iapi.sqoptrade.parameter.utill.Utils.calcTimeSpanMin(orderDetailPo
								.getInStockTime()) < 2, "验证数据库InStockTime跟系统当前时间差为2分钟内");
				
				num = num - vStockCount;
			} else {
				
				Logger.verifyEquals(true, orderPo.getWaitInStock(), "验证orderPo WaitInStock");
				Logger.verifyEquals(null, orderPo.getInStockTime(), "验证orderPo InStockTime");
				Logger.verifyEquals(true, orderDetailPo.getWaitInStock(), "验证订单详情上orderDetailPo WaitInStock");
				Logger.verifyEquals(null, orderDetailPo.getInStockTime(), "验证订单详情上orderDetailPo InStockTime");
				
				// 如果有一个订单因为库存不足不能核销，则后面所有订单均不能核销
				num = 0;
			}
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqStockChangeNotify_001() {
		Logger.start(true, "未支付订单不能核销");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp resp = SqTradingCallService.placeOrder1(productBean,
					autotest_goodsOwnerId, 2);
			
			SqTradingCallService.stockIn((int) autotest_goodsOwnerId,
					productBean.getRootProductId(), 10);

			sqStockChangeNotifyReq.setCatalogId(productBean.getRootCatalogList().get(0));
			sqStockChangeNotifyReq.setSellerId(productBean.getGoodsOwnerId());

			sqStockChangeNotifyCall.setData(sqStockChangeNotifyReq);
			sqStockChangeNotifyCall.callService();

			Logger.comment("前置数据准备结束");
			
			this.verify(Arrays.asList(resp.getOrderList().get(0).getOrderId()), 0);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqStockChangeNotify_002() {
		Logger.start(true, "已取消的订单不能核销");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService.placeOrder1(productBean,
					autotest_goodsOwnerId, 2);
			
			Long userId = placeOrderResp.getUserId();
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());
			
			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), userId.intValue(),
					BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);
			
			SqTradingCallService.cancelOrder(orderId, CancelOrderTypeEnum.SELLER_CANCEL_ORDER, autotest_goodsOwnerId);
			
			SqTradingCallService.stockIn((int) autotest_goodsOwnerId,
					productBean.getRootProductId(), 10);

			sqStockChangeNotifyReq.setCatalogId(productBean.getRootCatalogList().get(0));
			sqStockChangeNotifyReq.setSellerId(productBean.getGoodsOwnerId());

			sqStockChangeNotifyCall.setData(sqStockChangeNotifyReq);
			sqStockChangeNotifyCall.callService();

			Logger.comment("前置数据准备结束");
			
			this.verify(
					Arrays.asList(placeOrderResp.getOrderList().get(0)
							.getOrderId()), 0);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqStockChangeNotify_003() {
		Logger.start(true, "已支付的订单，未加实际库存，核销失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService.placeOrder1(productBean,
					autotest_goodsOwnerId, 2);
			
			Long userId = placeOrderResp.getUserId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());
			
			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), userId.intValue(),
					BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);

			sqStockChangeNotifyReq.setCatalogId(productBean.getRootCatalogList().get(0));
			sqStockChangeNotifyReq.setSellerId(productBean.getGoodsOwnerId());

			sqStockChangeNotifyCall.setData(sqStockChangeNotifyReq);
			sqStockChangeNotifyCall.callService();

			Logger.comment("前置数据准备结束");
			
			this.verify(Arrays.asList(placeOrderResp.getOrderList().get(0).getOrderId()), 0);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqStockChangeNotify_004() {
		Logger.start(true, "已支付的订单，加实际库存，核销成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService.placeOrder1(productBean,
					autotest_goodsOwnerId, 2);
			
			Long userId = placeOrderResp.getUserId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());
			
			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), userId.intValue(),
					BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);
			
			SqTradingCallService.stockIn((int) autotest_goodsOwnerId,
					productBean.getRootProductId(), 10);

			sqStockChangeNotifyReq.setCatalogId(productBean.getRootCatalogList().get(0));
			sqStockChangeNotifyReq.setSellerId(productBean.getGoodsOwnerId());

			sqStockChangeNotifyCall.setData(sqStockChangeNotifyReq);
			sqStockChangeNotifyCall.callService();

			Logger.comment("前置数据准备结束");
			
			this.verify(Arrays.asList(placeOrderResp.getOrderList().get(0).getOrderId()), 10);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqStockChangeNotify_005() {
		Logger.start(true, "多个订单，加实际库存，核销成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			
			// 第一个订单
			PlaceOrderReq placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(0, BigDecimal.ZERO, 20238699,
							productBean, autotest_goodsOwnerId, false, false);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			// 虚拟库存2
			orderItemVo.setProductNum(12);

			TradingServiceResp placeOrderResp = SqTradingCallService.callPlaceOrderService(placeOrderReq);
			
			GodmchManageCallService.setRiskAmount(productBean.getSellerId(), null, new BigDecimal("50000"));
			
			// 第二个订单
			PlaceOrderReq placeOrderReq1 = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(0, BigDecimal.ZERO, 20238699,
							productBean, autotest_goodsOwnerId, false, false);
			
			OrderItemVo orderItemVo1 = placeOrderReq1.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			// 虚拟库存1
			orderItemVo1.setProductNum(1);

			TradingServiceResp placeOrderResp1 = SqTradingCallService.callPlaceOrderService(placeOrderReq1);
			
			Long userId = placeOrderResp.getUserId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());
			
			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), userId.intValue(),
					BigDecimal.ZERO.toPlainString());
			
			// 支付订单
			Long thirdTradingId1 = SqTradingCallService.payOrder(userId,
					placeOrderResp1.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId1), placeOrderResp1
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), userId.intValue(),
					BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);
			
			SqTradingCallService.stockIn((int) autotest_goodsOwnerId,
					productBean.getRootProductId(), 10);

			sqStockChangeNotifyReq.setCatalogId(productBean.getRootCatalogList().get(0));
			sqStockChangeNotifyReq.setSellerId(productBean.getGoodsOwnerId());

			sqStockChangeNotifyCall.setData(sqStockChangeNotifyReq);
			sqStockChangeNotifyCall.callService();
			
			this.verify(
					Arrays.asList(placeOrderResp.getOrderList().get(0)
							.getOrderId(), placeOrderResp1.getOrderList()
							.get(0).getOrderId()), 10);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqStockChangeNotify_006() {
		Logger.start(true, "多个订单，加实际库存，实际库存不足以满足所有订单，部分订单核销成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			
			// 第一个订单
			TradingServiceResp placeOrderResp = SqTradingCallService.placeOrder1(productBean,
					autotest_goodsOwnerId, 1);
			
			Long userId = placeOrderResp.getUserId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());
			
			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), userId.intValue(),
					BigDecimal.ZERO.toPlainString());
			
			// 第二个订单
			PlaceOrderReq placeOrderReq2 = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(0, BigDecimal.ZERO, 20238699,
							productBean, autotest_goodsOwnerId, false, false);
			
			OrderItemVo orderItemVo2 = placeOrderReq2.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			// 虚拟库存1
			orderItemVo2.setProductNum(2);
			
			TradingServiceResp placeOrderResp2 = SqTradingCallService
					.callPlaceOrderService(placeOrderReq2);

			// 支付订单
			Long thirdTradingId2 = SqTradingCallService.payOrder(userId,
					placeOrderResp2.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId2), placeOrderResp2
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), userId.intValue(),
					BigDecimal.ZERO.toPlainString());

			// 第三个订单
			PlaceOrderReq placeOrderReq3 = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(0, BigDecimal.ZERO, 20238699,
							productBean, autotest_goodsOwnerId, false, false);
			
			OrderItemVo orderItemVo3 = placeOrderReq3.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			// 虚拟库存1
			orderItemVo3.setProductNum(1);
			
			TradingServiceResp placeOrderResp3 = SqTradingCallService
					.callPlaceOrderService(placeOrderReq3);

			// 支付订单
			Long thirdTradingI3 = SqTradingCallService.payOrder(userId,
					placeOrderResp3.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingI3), placeOrderResp3
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), userId.intValue(),
					BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);
			
			Logger.comment("前置数据准备结束");
			
			SqTradingCallService.stockIn((int) autotest_goodsOwnerId,
					productBean.getRootProductId(), 2);

			sqStockChangeNotifyReq.setCatalogId(productBean.getRootCatalogList().get(0));
			sqStockChangeNotifyReq.setSellerId(productBean.getGoodsOwnerId());

			sqStockChangeNotifyCall.setData(sqStockChangeNotifyReq);
			sqStockChangeNotifyCall.callService();
			
			this.verify(
					Arrays.asList(placeOrderResp.getOrderList().get(0)
							.getOrderId(), placeOrderResp2.getOrderList()
							.get(0).getOrderId(), placeOrderResp3.getOrderList()
							.get(0).getOrderId()), 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqStockChangeNotify_007() {
		Logger.start(true, "砍价团组团成功订单，加实际库存，核销成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			// 砍价团id
			String bizId = TestDataManager.getRandomNum(9);
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 2, true);

			BargainGroupService.shenqiOpenGroup(bizId, productBean, true);

			// 参团
			TradingServiceResp placeOrder2 = BargainGroupService
					.shenqiJoinGroup(bizId, productBean, true);

			long orderId = placeOrder2.getOrderList().get(0).getOrderId();

			TradingSyncService.getSyncResult(orderId);

			BargainGroupService.notifyOrderBizStatus(bizId, orderId, 2, 3);

			Thread.sleep(10000);

			SqTradingCallService.ReduceSqProductStocks(orderId);
			
			Logger.comment("前置数据准备结束");
			
			SqTradingCallService.stockIn((int) autotest_goodsOwnerId,
					productBean.getRootProductId(), 10);

			sqStockChangeNotifyReq.setCatalogId(productBean.getRootCatalogList().get(0));
			sqStockChangeNotifyReq.setSellerId(productBean.getGoodsOwnerId());

			sqStockChangeNotifyCall.setData(sqStockChangeNotifyReq);
			sqStockChangeNotifyCall.callService();
			
			this.verify(Arrays.asList(placeOrder2.getOrderList().get(0).getOrderId()), 10);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqStockChangeNotify_008() {
		Logger.start(true, "已支付的订单，一个订单两个商品都有虚拟库存，加实际库存，核销其中一个商品");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = new AgentProductBean();
			
			String productId = DGSQBaseService.createAgentProduct(
					(int) autotest_goodsOwnerId, "autotest_goodsOwnerId", true,
					"CNY", true);
			
			productBean.setSellerId(autotest_goodsOwnerId);
			productBean.setSellerLoginId(new DGUserWapper().selectDGUserInfo(
					(int)autotest_goodsOwnerId).get(0).getLoginId());
			productBean.setProductId(productId);
			productBean.setRootProductId(productId);
			productBean.setCatalogList(DGSQBaseService.getCatalogIdList(productId));
			productBean.setRootCatalogList(DGSQBaseService.getCatalogIdList(productId));
			productBean.setGoodsOwnerId(autotest_goodsOwnerId);
			
			PlaceOrderReq placeOrderReq = SqTradingCallService
					.buildDGSQStandPlaceOrderReq(0, BigDecimal.ZERO, 20238699,
							productBean, autotest_goodsOwnerId, false, false);
			
			OrderItemVo orderItemVo = placeOrderReq.getOrderGroups().get(0)
					.getOrders().get(0).getOrderItems().get(0);
			
			// 虚拟库存1
			orderItemVo.setProductNum(12);
			
			AgentProductBean productBean1 = new AgentProductBean();
			
			String productId1 = DGSQBaseService.createAgentProduct(
					(int) autotest_goodsOwnerId, "autotest_goodsOwnerId", true,
					"CNY", true);
			
			productBean1.setSellerId(autotest_goodsOwnerId);
			productBean1.setSellerLoginId(new DGUserWapper().selectDGUserInfo(
					(int)autotest_goodsOwnerId).get(0).getLoginId());
			productBean1.setProductId(productId1);
			productBean1.setRootProductId(productId1);
			productBean1.setCatalogList(DGSQBaseService.getCatalogIdList(productId1));
			productBean1.setRootCatalogList(DGSQBaseService.getCatalogIdList(productId1));
			productBean1.setGoodsOwnerId(autotest_goodsOwnerId);
			
			OrderItemVo orderItemVo2 = SqTradingCallService
					.createOrderItemVo(productId1, productBean1.getCatalogList()
							.get(0));
			orderItemVo2.setProductNum(12);
			
			placeOrderReq.getOrderGroups().get(0).getOrders().get(0)
					.getOrderItems().add(orderItemVo2);
			
			TradingServiceResp placeOrderResp = SqTradingCallService
					.callPlaceOrderService(placeOrderReq);
			
			Long userId = placeOrderResp.getUserId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());
			
			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), userId.intValue(),
					BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);
			
			SqTradingCallService.stockIn((int) autotest_goodsOwnerId,
					productBean.getRootProductId(), 10);

			sqStockChangeNotifyReq.setCatalogId(productBean.getCatalogList().get(0));
			sqStockChangeNotifyReq.setSellerId(productBean.getGoodsOwnerId());

			sqStockChangeNotifyCall.setData(sqStockChangeNotifyReq);
			sqStockChangeNotifyCall.callService();
			
			SqTradingCallService.stockIn((int) autotest_goodsOwnerId,
					productBean1.getRootProductId(), 10);

			sqStockChangeNotifyReq.setCatalogId(productBean1.getCatalogList().get(0));
			sqStockChangeNotifyReq.setSellerId(productBean1.getGoodsOwnerId());

			sqStockChangeNotifyCall.setData(sqStockChangeNotifyReq);
			sqStockChangeNotifyCall.callService();

			Logger.comment("前置数据准备结束");
			
			this.verify(Arrays.asList(placeOrderResp.getOrderList().get(0).getOrderId()), 10);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SqStockChangeNotify_009() {
		Logger.start(true, "已接单的订单，加实际库存，核销成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService.placeOrder1(productBean,
					autotest_goodsOwnerId, 2);
			
			Long userId = placeOrderResp.getUserId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());
			
			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), userId.intValue(),
					BigDecimal.ZERO.toPlainString());
			
			Thread.sleep(5000);
			
			SqTradingCallService.sellerAcceptOrder(placeOrderResp.getOrderList().get(0).getOrderId());
			
			SqTradingCallService.stockIn((int) autotest_goodsOwnerId,
					productBean.getRootProductId(), 10);		

			Logger.comment("前置数据准备结束");
			
			sqStockChangeNotifyReq.setCatalogId(productBean.getRootCatalogList().get(0));
			sqStockChangeNotifyReq.setSellerId(productBean.getGoodsOwnerId());

			sqStockChangeNotifyCall.setData(sqStockChangeNotifyReq);
			sqStockChangeNotifyCall.callService();
			
			this.verify(Arrays.asList(placeOrderResp.getOrderList().get(0).getOrderId()), 10);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) {
		
		Ts_SqStockChangeNotify test = new Ts_SqStockChangeNotify();
		
		sqStockChangeNotifyReq = new SqStockChangeNotifyReq();
		
		sqStockChangeNotifyReq.setCatalogId("c7167653");
		sqStockChangeNotifyReq.setSellerId(500002490L);
		
		test.verify(Arrays.asList(10829708699L), 10);
	}
}
